package com.nsl.my_youlai.bbu2.model.bo.bbu.rules;

import com.nsl.my_youlai.bbu2.enums.NetGenerationEnum;
import com.nsl.my_youlai.bbu2.enums.VendorEnum2;
import com.nsl.my_youlai.bbu2.enums.BoardTypeEnum;
import com.nsl.my_youlai.bbu2.model.bo.bbu.BbuBoard2;
import com.nsl.my_youlai.bbu2.model.bo.bbu.BbuRecordCheckError;
import com.nsl.my_youlai.bbu2.model.bo.bbu.BbuSlot2;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

//1个插槽对应一个规则
@Data
public class BbuSlotRule {
    //    private int index;
    private List<BoardTypeEnum> boardTypeEnumList=new ArrayList<>();

    private Integer boardSize;

    private List<NetGenerationEnum> canInjectNetGenerationEnumList = new ArrayList<>();

    private VendorEnum2 vendor;

    private List<String> canInstallBoardTypeList = new ArrayList<>();


    private String canInjectNetGenerationEnumStrings = null;

    public String getCanInjectNetGenerationEnumStrings() {
        if (boardTypeEnumList.contains(BoardTypeEnum.POWER_BOARD)) {
            canInjectNetGenerationEnumStrings = "";
        } else {
            if (canInjectNetGenerationEnumStrings == null) {
                String s = " [";
                for (NetGenerationEnum netGenerationEnum : canInjectNetGenerationEnumList) {
                    s += netGenerationEnum.getLabel() + ",";
                }
                s = s.substring(0, s.length() - 1) + "] ";
                canInjectNetGenerationEnumStrings = s;
            }
        }

        return canInjectNetGenerationEnumStrings;
    }


    //目前自己做方案的时候不考虑混插，即只考虑 同厂家，同网络类型的整合
    //校验的时候，考虑混插的情况
    public List<BbuRecordCheckError> check(BbuSlot2 slot) {
        List<BbuRecordCheckError> slotCheckList = new ArrayList();


        for (int i = 0; i < slot.getBbuBoardList().size(); i++) {
            BbuBoard2 board = slot.getBbuBoardList().get(i);

            //数量检查
            if (slot.getBbuBoardList().size() > boardSize) {
                BbuRecordCheckError checkError = new BbuRecordCheckError();
                checkError.setExcelRowIndex(board.getExcelRowIndex());
                checkError.setErrorType("插槽重复配置");
                checkError.setErrorMessage("插槽:" + slot.getSlotIndex() + " 只能插入" + this.boardSize + "块板卡,现配置为" + slot.getBbuBoardList().size() + "块板卡");
                slotCheckList.add(checkError);
            }

            //厂家一致性检查
            if (board.getVendor() != this.getVendor()) {
                BbuRecordCheckError checkError = new BbuRecordCheckError();
                checkError.setExcelRowIndex(board.getExcelRowIndex());
                checkError.setErrorType("厂家不匹配");
                checkError.setErrorMessage("插槽:" + slot.getSlotIndex() + "板卡厂家应为:" + this.vendor.getValue() + "，现配置为:" + board.getVendor().getValue());
                slotCheckList.add(checkError);
            }

            if (!boardTypeEnumList.contains(board.getBoardTypeEnum())) {
                BbuRecordCheckError checkError = new BbuRecordCheckError();
                checkError.setExcelRowIndex(board.getExcelRowIndex());
                checkError.setErrorType("板卡类型不匹配");
                checkError.setErrorMessage("插槽:" + slot.getSlotIndex() + "支持板卡类型:" + boardTypeRangString() + " ,现配置为" + board.getBoardTypeEnum().getLabel() + "板");
                slotCheckList.add(checkError);
            }


            if (board.getBoardTypeEnum() == BoardTypeEnum.BASE_BAND_BOARD || board.getBoardTypeEnum() == BoardTypeEnum.CONTROL_BOARD) {
                if (!this.canInjectNetGenerationEnumList.contains(board.getNetGenerationEnum())) {
                    BbuRecordCheckError checkError = new BbuRecordCheckError();
                    checkError.setExcelRowIndex(board.getExcelRowIndex());
                    checkError.setErrorType("网络类型不匹配");
                    checkError.setErrorMessage("插槽:" + slot.getSlotIndex() + "支持板卡网络类型:" + this.getCanInjectNetGenerationEnumStrings() + ", 现配置板卡网络为" + board.getNetGenerationEnum().getLabel());
                    slotCheckList.add(checkError);
                }
            }

//            if(!canInstallBoardTypeList.contains(board.getModelName())){
//                slotCheckList.add("该插槽不支持：型号为xxx的板卡");
//            }
        }
        return slotCheckList;
    }

    private String boardTypeRangString(){
        String s="[";
        for (BoardTypeEnum boardTypeEnum : boardTypeEnumList) {
            s+=boardTypeEnum.getLabel()+",";
        }
        s=s.substring(0,s.length()-1);
        s+="]";
        return s;
    }

}
